// pages/mch/car/list.js
const App = getApp()
import Kits from '../../../utils/kits'

Component({
    options: {
        addGlobalClass: true,
    },
    properties: {
        state: {
            type: String,
            default: '0'
        },
        temp: {
            type: String,
            default: ''
        },
    },
    data: {
        init: false,
        loading: false,
        triggered: true,
        CustomBar: App.Global.CustomBar,
        page: 1,
        size: 8,
        time: new Date().getTime(),
        pager: {
            now: 1,
            row: 0,
            all: 0,
            fms: 0,
        },
        items: []
    },

    lifetimes: {
        attached() {
            wx.sub('ReloadCarList', this, () => {
                this.execLoad(false)
            })
            this.execLoad(false)
        },
        detached() {
            wx.cancel('ReloadCarList', this)
        },
    },
    methods: {
        doPulldown() {
            this.data.page = 1
            this.execLoad(true)
        },
        toShareCar(e) {
            const index = e.currentTarget.dataset.index
            const params = {
                type: 'car_share',
                info: {
                    car: this.data.items[index],
                    mch: App.Global.User.merchant
                }
            }

            wx.navigateTo({
                url: '/pages/funcs/share?params=' + JSON.stringify(params)
            })
        },
        doReachBottom() {
            if (this.data.page >= this.data.pager.all) {
                return
            }
            this.data.page = this.data.page + 1
            this.execLoad(true)
        },
        fmtMoney(coin) {
            let yuan = coin / 100
            if (parseInt(yuan) * 100 < coin) {
                let s = yuan.toFixed(2)
                if (yuan.toFixed(2).endsWith('0')) {
                    return yuan.toFixed(1)
                }
                return s
            } else {
                return yuan.toFixed(0)
            }
        },
        toDetail(e) {
            wx.navigateTo({
                url: `/pages/mch/car/detail?cid=${e.currentTarget.dataset.id}`,
            })
        },
        execLoad(pulldown) {
            this.setData({
                loading: true
            })
            App.doPost({
                uri: '/app/merchant/car/list',
                body: {
                    pager: {
                        page: this.data.page,
                        size: this.data.size,
                        time: this.data.time
                    },
                    query: {
                        state: parseInt(this.data.state),
                        temp: this.data.temp != '' ? this.data.temp == 'true' : undefined
                    }
                },
                fnSucc: resp => {
                    if (resp.data.pager.now == 1) {
                        this.data.time = resp.data.pager.fms
                        this.triggerEvent('totalevent', {
                            type: this.data.state == '1' ? 'idle' : (this.data.state == '2' ? 'rund' : 'noup'),
                            value: resp.data.pager.row
                        }, {})
                    }
                    resp.data.items.forEach(item => {
                        item.price.pdpStr = Kits.fmtMoneyString(item.price.pdp)
                        item.price.pmpStr = Kits.fmtMoneyString(item.price.pmp)
                        item.price.mdpStr = Kits.fmtMoneyString(item.price.mdp)
                        item.price.depositStr = Kits.fmtMoneyString(item.price.deposit)
                        if(!item.cover && Array.isArray(item.photo) && item.photo.length){
                            item.cover = item.photo[0]
                        }
                    })

                    this.setData({
                        init: true,
                        loading: false,
                        triggered: false,
                        pager: resp.data.pager,
                        items: resp.data.pager.now == 1 ? resp.data.items : this.data.items.concat(resp.data.items)
                    })
                },
                fnComp: () => {
                    if (pulldown) {
                        this.triggerEvent('stopPullDown', {})
                    }
                }
            })
        },
    }
})